﻿Imports Microsoft.Win32

Public Class frmOptions

  Dim blnOptionsStartup As Boolean

  Private Sub frmOptions_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'Declare and Initialize Procedure-Level Variables and Constants
    Dim regKey As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True)
    Dim strRegistryValues() As String = regKey.GetValueNames
    blnOptionsStartup = True

    'Load computer info into Computers listview
    LoadComputerInfo()     'Sub/Function at end of frmOptions class

    'Set the polling interval and value of Polling Interval trackbar
    Me.tbrPollingInterval.Tag = intPollingInterval
    Me.tbrPollingInterval.Value = intPollingValue

    'Check the Registry and see if the DiskSpaceReporter value is present
    'If so, set chkWindowsStartup to TRUE
    For i As Integer = 0 To strRegistryValues.Count - 1
      If strRegistryValues(i) = "DiskSpaceReporter" Then
        Me.chkWindowsStartup.Checked = True
        Exit For
      End If
    Next

    blnOptionsStartup = False
  End Sub

  Private Sub cmdOptions_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOptions.Click
    'Get the location of the Options button and open the context menu directly below it
    Dim pos As Point = cmdOptions.Location
    pos.Y += cmdOptions.Height
    Me.cmsComputers.Show(Me, pos)
  End Sub

  Private Sub cmdMoveUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdMoveUp.Click
    'If an item in the Computers listview is selected
    If lvComputers.SelectedItems.Count = 1 Then
      'Make a new listview item of the currently selected listview item
      Dim lvItem As ListViewItem = lvComputers.SelectedItems.Item(0)
      'Get the index of the currently selected listview item
      Dim intIndexCurrent As Integer = lvComputers.SelectedItems.Item(0).Index
      'Subtract 1 from the index of the currently selected listview item
      Dim intIndexUp As Integer = intIndexCurrent - 1

      If intIndexCurrent > 0 Then     'If the selected listview item is not already at the top
        'Remove the currently selected listview item
        lvComputers.Items.RemoveAt(intIndexCurrent)
        'Add the listview item back into the listview at the indicated index
        lvComputers.Items.Insert(intIndexUp, lvItem)
        'Redo the background coloring of the listview items
        For i As Integer = 0 To lvComputers.Items.Count - 1
          If i Mod 2 = 0 Then
            lvComputers.Items.Item(i).BackColor = Color.White
          Else
            lvComputers.Items.Item(i).BackColor = Color.FromArgb(232, 237, 248)
          End If
        Next
      Else     'If the selected listview item is already at the top
        MessageBox.Show("Selected item is already at the top of the list.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
      End If
    End If
  End Sub

  Private Sub cmdMoveDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdMoveDown.Click
    'If an item in the Computers listview is selected
    If lvComputers.SelectedItems.Count = 1 Then
      'Make a new listview item of the currently selected listview item
      Dim lvItem As ListViewItem = lvComputers.SelectedItems.Item(0)
      'Get the index of the currently selected listview item
      Dim intIndexCurrent As Integer = lvComputers.SelectedItems.Item(0).Index
      'Add 1 to the index of the currently selected listview item
      Dim intIndexDown As Integer = intIndexCurrent + 1

      If intIndexDown < lvComputers.Items.Count Then     'If the selected listview item is not already at the bottom
        'Remove the currently selected listview item
        lvComputers.Items.RemoveAt(intIndexCurrent)
        'Add the listview item back into the listview at the indicated index
        lvComputers.Items.Insert(intIndexDown, lvItem)
        'Redo the background coloring of the listview items
        For i As Integer = 0 To lvComputers.Items.Count - 1
          If i Mod 2 = 0 Then
            lvComputers.Items.Item(i).BackColor = Color.White
          Else
            lvComputers.Items.Item(i).BackColor = Color.FromArgb(232, 237, 248)
          End If
        Next
      Else     'If the selected listview item is already at the bottom
        MessageBox.Show("Selected item is already at the bottom of the list.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
      End If
    End If
  End Sub

  Private Sub cmsComputers_VisibleChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmsComputers.VisibleChanged
    'Change the Options button background image based on whether the context menu is visible or not
    If Me.cmsComputers.Visible = True Then
      Me.cmdOptions.BackgroundImage = My.Resources.buttonOptionsDropdownOnClick
    Else
      Me.cmdOptions.BackgroundImage = My.Resources.buttonOptionsDropdown
    End If
  End Sub

  Private Sub EditComputerToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditComputerToolStripMenuItem.Click
    Try
      'Set the Edit Computer form ID field with the currently selected listview item from Computers
      frmAddEditComputer.txtID.Text = Me.lvComputers.SelectedItems.Item(0).Index
      'Open the Edit Computer form
      frmAddEditComputer.ShowDialog()
      'Reload the computers in the Computers listview
      LoadComputerInfo()
    Catch ex As Exception
      'If no computer was selected...
      MessageBox.Show("Please select a computer from the list to edit.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
    End Try
  End Sub

  Private Sub AddComputerToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddComputerToolStripMenuItem.Click
    'Open the Add Computer form
    frmAddEditComputer.ShowDialog()
    'Reload the computers in the Computers listview
    LoadComputerInfo()
  End Sub

  Private Sub DeleteComputerToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteComputerToolStripMenuItem.Click
    'Remove the currently selected listview item from Computers from the Computers arraylist
    arlComputers.RemoveAt(Me.lvComputers.SelectedItems.Item(0).Index)
    'Remove the currently selected listview item from Computers from the Computers listview
    Me.lvComputers.Items.RemoveAt(Me.lvComputers.SelectedItems.Item(0).Index)
    'Reload the computers in the Computers listview
    LoadComputerInfo()
  End Sub

  Private Sub tbrPollingInterval_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbrPollingInterval.Scroll
    'Set the number of seconds and the value of the Polling Interval trackbar
    Select Case Me.tbrPollingInterval.Value
      Case 1
        Me.tbrPollingInterval.Tag = 300000
        Exit Select
      Case 2
        Me.tbrPollingInterval.Tag = 900000
        Exit Select
      Case 3
        Me.tbrPollingInterval.Tag = 1800000
        Exit Select
      Case 4
        Me.tbrPollingInterval.Tag = 3600000
        Exit Select
      Case 5
        Me.tbrPollingInterval.Tag = 7200000
        Exit Select
      Case 6
        Me.tbrPollingInterval.Tag = 14400000
        Exit Select
    End Select
    'Set the global variables to the selected number of seconds and value
    intPollingInterval = Me.tbrPollingInterval.Tag
    intPollingValue = Me.tbrPollingInterval.Value
  End Sub

  Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
    If CreateOptionsFile(False) = True Then    'If the options were written to the options file successfully, close the Options form
      Me.Close()
    Else   'If not...
      Dim blnSuccess As Boolean = False
      'Keep asking the user if they wish to try to write the options file again
      'and trying to write it if they answer yes, or close the Options form if they answer no or the write is successful
      Do Until blnSuccess = True
        If MessageBox.Show("There was an error creating the DiskSpaceReporter Options file. Would you like to try to create the file again?", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) = DialogResult.Yes Then
          blnSuccess = CreateOptionsFile(False)
        Else
          blnSuccess = True
        End If
      Loop
    End If
    Me.Close()
  End Sub

  Private Sub LoadComputerInfo()
    'Clear the Computers listview
    Me.lvComputers.Items.Clear()
    'Loop through the Computers arraylist, adding each computer within to the Computers listview
    For i As Integer = 0 To arlComputers.Count - 1
      Dim strComputerInfo() As String = arlComputers.Item(i)
      Dim lvItem As New ListViewItem(i)
      lvItem.SubItems.AddRange(strComputerInfo)
      'Set the background coloring of the listview item
      If i Mod 2 = 0 Then
        lvItem.BackColor = Color.White
      Else
        lvItem.BackColor = Color.FromArgb(232, 237, 248)
      End If
      Me.lvComputers.Items.Add(lvItem)
    Next
  End Sub

  Private Sub ImportOptionsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ImportOptionsToolStripMenuItem.Click
    If ImportExportOptionsFile() = True Then     'If the file exists...
      If MessageBox.Show("Import successful." & vbNewLine & vbNewLine & "Would you like to check the imported drives now?", Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = DialogResult.Yes Then
        'Load the Computers arraylist with the computers from the options file to check
        LoadComputersCollection(Application.StartupPath & "\dsrOptions.xml")
        'Load the computers into the Computers listview
        LoadComputerInfo()
        'Check the computers in the Computers arraylist
        CheckSizes()
      End If
    Else
      MessageBox.Show("There was an error importing the specified Options file.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
    End If
  End Sub

  Private Sub ExportOptionsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExportOptionsToolStripMenuItem.Click
    If ImportExportOptionsFile(False) = True Then     'If the file exists...
      MessageBox.Show("Export successful.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
    Else
      MessageBox.Show("There was an error exporting the specified Options file.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
    End If
  End Sub

  Private Sub chkWindowsStartup_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkWindowsStartup.CheckedChanged
    If blnOptionsStartup = False Then
      Dim regKey As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True)
      Dim strRegistryValues() As String = regKey.GetValueNames
      If Me.chkWindowsStartup.Checked = True Then
        For i As Integer = 0 To strRegistryValues.Count - 1
          If strRegistryValues(i) = "DiskSpaceReporter" Then
            regKey.SetValue("DiskSpaceReporter", Application.ExecutablePath)
            Exit Sub
          End If
        Next
        regKey.SetValue("DiskSpaceReporter", Application.ExecutablePath)
      Else
        For i As Integer = 0 To strRegistryValues.Count - 1
          If strRegistryValues(i) = "DiskSpaceReporter" Then
            regKey.DeleteValue("DiskSpaceReporter")
            Exit Sub
          End If
        Next
      End If
    End If
  End Sub

End Class